package dao.oracle;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bean.JenisKeretaApiBean;
import bean.loader.JenisKeretaApiLoader;
import dao.DAOFactory;
import dao.ResourceManager;

public class JenisKeretaApiDao {
	private DAOFactory factory;
	
	public JenisKeretaApiDao(DAOFactory factory) {
		super();
		this.factory = factory;
	}
	public JenisKeretaApiBean getJenisLokoWhereIdLoko(String idLoko) throws SQLException{
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			conn = factory.getConnection();
			ps = conn.prepareStatement("SELECT * FROM JENIS_KERETA_API WHERE ID_JENIS_KA = ?");
			ps.setString(1, idLoko);
			rs = ps.executeQuery();
			if(rs.next())
				return new JenisKeretaApiLoader().loadSingle(rs);
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SQLException(e);
		} finally{
			ResourceManager.close(rs);
			ResourceManager.close(ps);
		}
	}
	public List<String> getListJenisKeretaApi() throws SQLException{
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			conn = factory.getConnection();
			ps = conn.prepareStatement("SELECT DISTINCT ID_JENIS_KA FROM JENIS_KERETA_API ORDER BY ID_JENIS_KA ASC");
			rs = ps.executeQuery();
			List<String> result = new ArrayList<String>();
			while(rs.next()){
				result.add(rs.getString("ID_JENIS_KA"));
			}
			return result;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SQLException(e);
		} finally{
			ResourceManager.close(rs);
			ResourceManager.close(ps);
		}
	}
}
